using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using Cobalt75.GreyMatter;
using Cobalt75.GreyMatter.MySql;
using Cobalt75.GreyMatter.FirebirdSql;
using Cobalt75.GreyMatter.Tester.TestTypes;

namespace Cobalt75.GreyMatter.Tester
{
    public partial class TesterVisual : Form
    {
        public TesterVisual()
        {
            InitializeComponent();
            mywDel = new wDel(w);
            mytDel = new tDel(t);
        }
        wDel mywDel;
        tDel mytDel;
        Thread mT;
        private void button1_Click(object sender, EventArgs e)
        {
            mT = new Thread(new ThreadStart(Test));
            mT.Start();

        }
        void w(string s, params object[] ps)
        {
            this.textBox1.Text = this.textBox1.Text +
                String.Format(s, ps);
            this.textBox1.SelectionStart = this.textBox1.TextLength;
            this.textBox1.ScrollToCaret();
        }
        void t(int p, int pm)
        {
            if (p == -1)
            {

                this.progressBar1.Style = ProgressBarStyle.Marquee;
            }
            else
            {
                this.progressBar1.Style = ProgressBarStyle.Continuous;
                this.progressBar1.Maximum = pm;
                this.progressBar1.Value = p;
            }
        }
        void writeline()
        {
            BeginInvoke(mywDel, Environment.NewLine,new object[0]);
        }
        void writeline(string s, params object[] ps)
        {
            BeginInvoke(mywDel, s + Environment.NewLine, ps);
        }
        void write(string s, params object[] ps)
        {
            BeginInvoke(mywDel, s, ps);
        }
        void think(int p, int pm)
        {

            BeginInvoke(mytDel, p, pm);
        }

        delegate void wDel(string s, params object[] ps);
        delegate void tDel(int p, int pm);
        void Test()
        {

            writeline("Creating gmc's");
            think(-1, 0);
            GMCommunicator gmc;
            //gmc= new MySqlGMCommunicator("gmtest", "gmtest", "gmtest");


            //File.Delete("C:\\gmtest.fdb");

            //gmc = new FirebirdSqlGMCommunicator("C:\\", "gmtest");

            writeline("Creating gmc's");
            think(-1, 0);
            gmc = new MySqlGMCommunicator("gmtest", "gmtest", "gmtest");

            TestTypeA a_Low;
            TestTypeA b_Low;
            TestTypeA LowLookup;
            TestTypeA a_High;
            TestTypeA b_High;
            TestTypeA HighLookup;



            gmc.CreateDBSchemaAndReport();
            gmc.GenerateReflectionCache();

            writeline("Creating (A) Low");

            think(1, 100);
            a_Low = new TestTypeA(gmc);
            a_Low.TextA = TestingResources.StringTests_Pangrams_Hebrew;
            a_Low.TextB = TestingResources.StringTests_Pangrams_Hiragana;
            a_Low.TextC = TestingResources.StringTests_Pangrams_Kanji;
            a_Low.TextD = TestingResources.StringTests_Poems_PolytonicGreek;
            a_Low.TextE = "short test";
            rep(a_Low);
            gmc.CreateDB(a_Low);
            think(10, 100);

            writeline("Creating (A) High");
            a_High = new TestTypeA(gmc);
            a_High.Boolean = true;
            a_High.Byte = Byte.MaxValue;
            a_High.Char = Char.MaxValue;
            a_High.Decimal = Decimal.MaxValue;

            a_High.Double = double.Parse("1.79769313486231E+308");

            a_High.EnumDayOfWeek = DayOfWeek.Sunday;
            a_High.Float = float.MaxValue;
            a_High.Int16 = Int16.MaxValue;
            a_High.Int32 = Int32.MaxValue;
            a_High.Int64 = Int64.MaxValue;
            a_High.SByte = SByte.MaxValue;

            a_High.UInt16 = UInt16.MaxValue;
            a_High.UInt32 = UInt32.MaxValue;

            a_High.UInt64 = UInt64.MaxValue;

            a_High.TextA = TestingResources.StringTests_Pangrams_Hebrew;
            a_High.TextB = TestingResources.StringTests_Pangrams_Hiragana;
            a_High.TextC = TestingResources.StringTests_Pangrams_Kanji;
            a_High.TextD = TestingResources.StringTests_Poems_PolytonicGreek;
            a_High.TextE = "short test";
            rep(a_High);
            gmc.CreateDB(a_High);
            think(20, 100);



            LowLookup = gmc.SelectFromId<TestTypeA>(1);
            writeline("Lookup (A) Low");
            rep(LowLookup);
            think(30, 100);

            HighLookup = gmc.SelectFromId<TestTypeA>(2);
            writeline("Lookup (A) High");
            rep(HighLookup);
            think(40, 100);


            writeline();
            writeline();
            writeline();
            writeline("Match testing Low");
            MatchTest(a_Low, LowLookup);
            think(50, 100);
            writeline("Match testing High");
            MatchTest(a_High, HighLookup);
            think(100, 100);

        }


        void MatchTest(TestTypeA a, TestTypeA a2)
        {
            writeline("Boolean        = {0} ({1})", a.Boolean == a2.Boolean, a.Boolean);
            writeline("Byte           = {0} ({1})", a.Byte == a2.Byte, a.Byte);
            writeline("Char           = {0} ({1})", a.Char == a2.Char, a.Char);
            writeline("Char as int    = {0} ({1})", (int)a.Char == (int)a2.Char, (int)a.Char);
            writeline("DateCreated    = {0} ({1})", a.DateCreated == a2.DateCreated, a.DateCreated);
            writeline("DateModified   = {0} ({1})", a.DateModified == a2.DateModified, a.DateModified);
            writeline("Decimal        = {0} ({1})", a.Decimal == a2.Decimal, a.Decimal);
            writeline("Double         = {0} ({1})", a.Double == a2.Double, a.Double);
            writeline("EnumDayOfWeek  = {0} ({1})", a.EnumDayOfWeek == a2.EnumDayOfWeek, a.EnumDayOfWeek);
            writeline("Float          = {0} ({1})", a.Float == a2.Float, a.Float);
            writeline("Int16          = {0} ({1})", a.Int16 == a2.Int16, a.Int16);
            writeline("Int32          = {0} ({1})", a.Int32 == a2.Int32, a.Int32);
            writeline("Int64          = {0} ({1})", a.Int64 == a2.Int64, a.Int64);
            writeline("SByte          = {0} ({1})", a.SByte == a2.SByte, a.SByte);
            writeline("TextA          = {0} ({1})", a.TextA == a2.TextA, a.TextA);
            writeline("TextB          = {0} ({1})", a.TextB == a2.TextB, a.TextB);
            writeline("TextC          = {0} ({1})", a.TextC == a2.TextC, a.TextC);
            writeline("TextD          = {0} ({1})", a.TextD == a2.TextD, a.TextD);
            writeline("TextE          = {0} ({1})", a.TextE == a2.TextE, a.TextE);
            writeline("UInt16         = {0} ({1})", a.UInt16 == a2.UInt16, a.UInt16);
            writeline("UInt32         = {0} ({1})", a.UInt32 == a2.UInt32, a.UInt32);
            writeline("UInt64         = {0} ({1})", a.UInt64 == a2.UInt64, a.UInt64);
        }
        void rep(TestTypeA a2)
        {

            writeline("Id             = {0}", a2.Id);
            writeline("Boolean        = {0}", a2.Boolean);
            writeline("Byte           = {0}", a2.Byte);
            writeline("Char           ='{0}'", a2.Char);
            writeline("Char as int    = {0} ", (int)a2.Char);
            writeline("DateCreated    = {0}", a2.DateCreated);
            writeline("DateModified   = {0}", a2.DateModified);
            writeline("Decimal        = {0}", a2.Decimal);
            writeline("Double         = {0}", a2.Double);
            writeline("EnumDayOfWeek  = {0}", a2.EnumDayOfWeek);
            writeline("Float          = {0}", a2.Float);
            writeline("Int16          = {0}", a2.Int16);
            writeline("Int32          = {0}", a2.Int32);
            writeline("Int64          = {0}", a2.Int64);
            writeline("SByte          = {0}", a2.SByte);
            writeline("TextA          = {0}", a2.TextA);
            writeline("TextB          = {0}", a2.TextB);
            writeline("TextC          = {0}", a2.TextC);
            writeline("TextD          = {0}", a2.TextD);
            writeline("TextE          = {0}", a2.TextE);
            writeline("UInt16         = {0}", a2.UInt16);
            writeline("UInt32         = {0}", a2.UInt32);
            writeline("UInt64         = {0}", a2.UInt64);
        }
        string TheKillerString()
        {
            char[] chars = new char[1];
            byte[] utf7bytes;
            byte[] utf8bytes;
            byte[] unibytes;
            for (int i = 0; i < Char.MaxValue; i++)
            {

                chars[0] = (char)i;

                writeline("Char\t{0}\t{1}", i, chars[0]);

                utf7bytes = System.Text.Encoding.UTF7.GetBytes(chars);
                utf8bytes = System.Text.Encoding.UTF8.GetBytes(chars);
                unibytes = System.Text.Encoding.Unicode.GetBytes(chars);
                for (int j = 0; j < utf7bytes.Length; j++)
                {
                    write("{0} ", utf7bytes[j]);
                }
                writeline();
                for (int j = 0; j < utf8bytes.Length; j++)
                {
                    write("{0} ", utf8bytes[j]);
                }
                writeline();
                for (int j = 0; j < unibytes.Length; j++)
                {
                    write("{0} ", unibytes[j]);
                }
                writeline();

            }
            return "";
        }
    
    }
}